package com.android;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.Model.Pojo.Users;
import com.sql.DAO;

import com.Model.Date.DateTime;

import com.Model.denglu.PanDuanZhangHao;

public class CunQvKuanServlet extends HttpServlet {

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 *
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		request.setCharacterEncoding("UTF-8");//编码格式
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();

		String name = request.getParameter("username");
		String pass = request.getParameter("password");
		String shenfen = request.getParameter("shenfen");
		PanDuanZhangHao panduan = new PanDuanZhangHao(name, pass, shenfen); //实例化判断账号密码的对象
		String flag = panduan.panDuan(); //判断账号密码正确并获取返回值
		String sql = panduan.pingsql(flag);//拼接sql语句
		DAO dao = new DAO();
		ArrayList<Users> list = new ArrayList<Users>();
		Users usermoney = new Users();
		list = dao.chaxun(sql); //查询账号密码对应的详细信息
		for(Users user : list){ //将信息存在user里面
			usermoney = (Users) user;
		}

		String type = request.getParameter("type"); //判断用户点击了哪个from的标志

		int cunmoney = 0; //存款或者取款的金额
		String str; //存款或者取款的字符串
		String style;
		String style1;
		String value;
		String source; //来源备注信息
		if(type.equals("cun")){
			str = (String)request.getParameter("money");
			cunmoney = Integer.parseInt(str);
			style = "1";
			style1 = "存款";
			source = "存入余额";
		}else{
			str = (String)request.getParameter("money");
			cunmoney = Integer.parseInt(str);
			cunmoney *= -1;
			style = "0";
			style1 = "取款";
			source = "余额取出";
		}


		int money = usermoney.getYue();//获取用户的账户余额
		int cash = usermoney.getMoney();//获取用户的现金
		money += cunmoney;
		cash -= cunmoney;
		String usersname = usermoney.getZhanghao();
		sql = "update users set yue='" + money + "' where name='" + usersname + "' ";
		dao.xiugai(sql);//更新users表中用户的账户余额
		sql = "update users set money='" + cash + "' where name='" + usersname + "' ";
		dao.xiugai(sql);//更新users表中用户的现金

		if(style.equals("0")){
			cunmoney *= -1;
		}
		DateTime date = new DateTime();
		sql = "insert into cunkuanqvkuan (name,money,style,date,yue)value('" + usersname + "','" + cunmoney
				+ "','" + style + "','" + date.getSettime() + "','" + money + "')";
		dao.zengjia(sql);

		sql = "insert into moneylist (name,style,value,date,cash,source)value('" + usersname + "','" + style1
				+ "','" + cunmoney + "','" + date.getSettime() + "','" + cash + "','" + source + "')";
		dao.zengjia(sql);

		out.print("success");

		out.flush();
		out.close();
	}

}
